//
//  HXQYPictureCollectionView.m
//  KD
//
//  Created by 梅洋 on 16/7/29.
//  Copyright © 2016年 Whx. All rights reserved.
//

#define kJianXiCell 10

#import "HXQYPictureCollectionView.h"
#import "HXHXQYPictureCollectionViewCell.h"

@interface HXQYPictureCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{

    NSIndexPath * _longIndexPath;
}

@property (nonatomic,assign) HXCollectionViewType type;

@end

@implementation HXQYPictureCollectionView

-(UICollectionViewFlowLayout *)loadCreateFlowLayout{

    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    //距离上下左右的最小距离
    layout.minimumLineSpacing      = kJianXiCell;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection         = UICollectionViewScrollDirectionHorizontal;//滑动方向
    
    return layout;
}

/**重写init方法  1.frame  2.长按手势是否能使用*/
-(instancetype)initWithFrame:(CGRect)frame withIsUsed:(BOOL)isUsed withHXCollectionViewType:(HXCollectionViewType)type{

    self = [super initWithFrame:frame collectionViewLayout:[self loadCreateFlowLayout]];
    
    if (self) {
        
        self.dataSource = self;
        self.delegate   = self;
        self.backgroundColor = [UIColor whiteColor];
        
        _dataArray = [[NSMutableArray alloc] init];
        
        self.type = type;
        self.isUsed = isUsed;
        
        //注册单元格
        [self registerClass:[HXHXQYPictureCollectionViewCell class] forCellWithReuseIdentifier:@"HXHXQYPictureCollectionViewCell"];
    }
    
    return self;
}
-(void)awakeFromNib{
    [super awakeFromNib];
    self.dataSource = self;
    self.delegate   = self;
    self.backgroundColor = [UIColor whiteColor];
    //注册单元格
    [self registerClass:[HXHXQYPictureCollectionViewCell class] forCellWithReuseIdentifier:@"HXHXQYPictureCollectionViewCell"];
}
-(void)setIsUsed:(BOOL)isUsed{
    _isUsed = isUsed;
    if (_isUsed) {
        
        UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAct:)];
        longPressGesture.minimumPressDuration = 1.0;
        [self addGestureRecognizer:longPressGesture];
    }
}


//长按手势
-(void)longPressGestureAct:(UILongPressGestureRecognizer *)longPress{

    NSMutableArray * picDataArr = [NSMutableArray arrayWithArray:_dataArray.copy];
    
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        CGPoint point = [longPress locationInView:self];
        _longIndexPath = [self indexPathForItemAtPoint:point];
        if (_longIndexPath == nil) {
            
            return;
        }else{
        
            [LTAlertView showTitle:@"是否删除照片" message:nil ButtonTitles:@[@"取消",@"确定"] OnTapBlock:^(LTAlertView *alert, NSInteger num) {
                if (num == 1) {
                    
                    [picDataArr removeObjectAtIndex:_longIndexPath.row];
                    _arrayBlock(picDataArr,_longIndexPath.row);
                }
            }];
        }
    }
}

/**block 长按时删除操作*/
-(void)ReturnDeleteDataArray:(ReturnDeleteDataArray)arrayBlock{

    _arrayBlock = arrayBlock;
}

-(void)setDataArray:(NSMutableArray<HXJobPunishDetailsListModel *> *)dataArray{

    _dataArray = dataArray;
    
    [self reloadData];
}

#pragma mark ----- // UICollectionViewDataSource \\ --------
//返回单元格的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataArray.count;
}
//创建单元格
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HXHXQYPictureCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HXHXQYPictureCollectionViewCell" forIndexPath:indexPath];
    
    HXJobPunishDetailsListModel * listModel = self.dataArray[indexPath.row];
    
    //图片
    if ([listModel.Img_Url isEqualToString:@"false"]) {
        
        cell.imgView.image = [UIImage imageWithData:[NSData base64DataFromString:listModel.Con_PicUrl]];
    }else if ([listModel.Img_Url isEqualToString:@"true"]){
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:listModel.Con_PicUrl] placeholderImage:[UIImage imageNamed:@"loading"]];
    }

    return cell;
}
//距离四周的距离
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    //top left bottom right
    return UIEdgeInsetsMake(0, kJianXiCell, 0, kJianXiCell);
}

//返回单元格的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake((self.frame.size.width-3*kJianXiCell)/3.8, (self.frame.size.width-3*kJianXiCell)/3.8);
}

//点击单元格
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    HXJobPunishDetailsListModel * listModel = self.dataArray[indexPath.row];

    _block(listModel);
}

-(void)SelectReturnTapCell:(SelectReturnTapCell)block{

    _block = block;
}

@end
